package com.feicheng.iou.util;

import com.feicheng.iou.constant.SessionConstant;
import com.feicheng.iou.entity.RolePermission;
import com.feicheng.iou.entity.User;
import com.feicheng.iou.mapper.PermissionMapper;
import com.feicheng.iou.mapper.RolePermissionMapper;
import com.feicheng.iou.mapper.UserMapper;
import com.feicheng.iou.mapper.UserRoleMapper;
import com.feicheng.iou.pojo.login.LoginReturnPOJO;
import com.feicheng.iou.pojo.login.PermissionLevelTwoPOJO;
import com.feicheng.iou.pojo.login.PermissionListPOJO;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author wuweinan
 * @DateTime 2021/8/17
 * @Des 权限工具类
 **/
public class PermissionUtil {

    /**
     * @param
     * @return ResultInfoV110
     * @Author wuweinan
     * @Description 查询出此人是否有对应权限,如果有则返回1或者比1大的数,没有则返回0
     * @Date 2021-8-9
     */
    public  static Integer permissionCount(HttpServletRequest request,String permissionName){
        //统计可分配权限个数
        Integer distributionCount = 0;
        //从Session中获取用户信息
        LoginReturnPOJO loginReturnPOJO =
                (LoginReturnPOJO) request.getSession().getAttribute(SessionConstant.CURRENT);
        //判断是否有可分配权限
        for (PermissionListPOJO permission : loginReturnPOJO.getPermissions()) {
            for (PermissionLevelTwoPOJO permissionPermission : permission.getPermissions()) {
                if (permissionPermission.getName().equals(permissionName)) {
                    distributionCount++;
                }
            }
        }
    return distributionCount;
    }


}
